package xy.web.timer;

import org.apache.log4j.Logger;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import xy.web.util.FileUtil;


public final class TimerLoader {
	private TimerLoader(){
		
	}
	private static TimerLoader instance = new TimerLoader();
	
	public static void load(String xml) throws Exception{
		instance.loadList(xml);
	}
	protected Logger log = Logger.getLogger(TimerLoader.class);

	protected void loadList(String xml) throws Exception {
		TaskList.timeList.clear();
		
		Element root = FileUtil.getXmlRoot(xml);

		NodeList items = root.getChildNodes();

		int size = items.getLength();
		for (int i = 0; i < size; i++) {
			Node item = items.item(i);	//action
			if(!item.getNodeName().equals("action")){
				continue;
			}
			String clsPath = item.getTextContent().trim();
			if(clsPath.length()==0){
				continue;
			}
			
			Class<?> cls = Class.forName(clsPath);

			AbstractTask task = (AbstractTask)cls.newInstance();
			
			NamedNodeMap attr = item.getAttributes();

			try{
				String time=attr.getNamedItem("time").getNodeValue().trim();
				task.time = "|"+time+"|";
				TaskList.timeList.add(task);
				
			}catch(Exception e){
				
				try{
					String period=attr.getNamedItem("period").getNodeValue().trim();
					task.period = period;
					TaskList.periodList.add(task);
				}catch(Exception e2){
					
				}
				
			}


			log.info("load timerAction: "+clsPath);
		}
	}
}
